<?php

namespace app\admin\controller\setting;

use app\admin\validate\setting\SystemConfigValidate;
use app\common\controller\AdminController;
use app\Request;
use app\services\system\config\SystemConfigService;
use app\services\system\config\SystemConfigTabService;
use think\App;

/**
 * 系统配置
 * Class SystemConfig
 * @package app\admin\controller\system\config
 */
class SystemConfig extends AdminController
{
    private SystemConfigService $services;

    /**
     * SystemConfig constructor.
     *
     * @param App                 $app
     * @param SystemConfigService $services
     */
    public function __construct(App $app, SystemConfigService $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 修改状态
     *
     * @param $id
     * @param $status
     *
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) {
            $this->error('参数错误');
        }
        $this->services->update($id, ['status' => $status]);
        \services\CacheService::clear();
        $this->success($status == 0 ? '隐藏成功' : '显示成功');
    }

    public function basicsConfigList()
    {
        $tabId = $this->request->get('tab_id', 0);
        if (!$tabId) {
            $this->error('参数错误');
        }
        $this->success($this->services->getBasicConfig($tabId));
    }

    /**
     * 保存数据
     * */
    public function saveBasics()
    {
        $post = $this->request->post();
        foreach ($post as $k => $v) {
            if (is_array($v)) {
                $res = $this->services->getUploadTypeList($k);
                foreach ($res as $kk => $vv) {
                    if ($kk == 'upload') {
                        if ($vv == 1 || $vv == 3) {
                            $post[$k] = $v[0];
                        }
                    }
                }
            }
        }
        validate(SystemConfigValidate::class)->check($post);
        if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
            $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
            if ($num > 100) {
                $this->error('一二级返佣比例不能大于100%');
            }
        }
        if (isset($post['spread_banner'])) {
            $num = count($post['spread_banner']);
            if ($num > 5) {
                $this->error('分销海报不能多于5张');
            }
        }
        if (isset($post['site_url'])) {
            if (!check_link($post['site_url'])) {
                $this->error('请输入正确的网站地址');
            }
            if (substr($post['site_url'], -1) == '/' || substr($post['site_url'], -1) == '\\') {
                $this->error('网站地址不能以/或\\结尾');
            }
        }
        foreach ($post as $k => $v) {
            if ($k === 'user_extract_min_price' && !preg_match('/[0-9]$/', $v)) {
                $this->error('提现最低金额只能为数字!');
            }
            // $config_one = $this->services->getOne(['menu_name' => $k]);
            // if ($config_one) {
            //     $config_one['value'] = $v;
            //     $this->services->valiDateValue($config_one);
            $this->services->update($k, ['value' => trim($v)], 'menu_name');
            // }
        }
        \services\CacheService::clear();
        $this->success('修改成功');

    }

    /**
     * Description: 获取系统设置头部分类
     *
     * @param SystemConfigTabService $services
     *
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function basicHeader(SystemConfigTabService $services)
    {
        [$pid, $isChildren, $status] = $this->request->params([
            [['pid', 'd'], 0],
            ['is_children', 1],
            [['status', 'd'], 1]
        ], true);
        $this->success($services->getConfigTab($pid, $isChildren, $status));
    }

}
